const http = require('../../utils/http.js');
const app = getApp();
const regeneratorRuntime = require("../../utils/regenerator-runtime/runtime-module.js");
Page({

	/**
	 * 页面的初始数据
	 */
  data: {

  },

  //金币现金兑换比例
  getExchange() {
    http.fetchApi('procedure_t', { p: 'R2008003' }).then(res => {
      if (res.data.code == 1) {
        var result = res.data.results[0];
        var proportion = result.name; //换算比例

        this.setData({
          proportion
        })
      }
    })
  },
  //支付
  payment(e) {

    var uid = app.globalData.uid,
      orderId = this.data.ddId,
      total = this.data.total,
     // total = 0.01,
      payType = e.currentTarget.dataset.type,
      //	redid = wx.getStorageSync('hongbao').pid || 0;
      redid = this.data.redid;
    console.log(typeof total)
    if (this.data.isXiaofei == 1) {
      //加小费
  console.log('xiaofei')
      if (payType == 5001) {
        http.fetchApi('api/zf', { jes: total * 100, lx: 2, uid, order_id: this.data.orderId}).then(payParmms => {
          var t = payParmms.data.timestampss,
            n = payParmms.data.nonceStrss,
            p = payParmms.data.packagess,
            s = payParmms.data.signTypess,
            ps = payParmms.data.paySignss;
          http.payment(t, n, p, s, ps).then(() => {
            http.fetchApi('procedure_t', {
              p: 'R2007002|' + uid + '|' + total + '|' + orderId + '|' + payType
            }).then(res => {
              if (res.data.code == 1) {
                if (res.data.results[0].codes == 1) {


                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
                  })


                } else {
                  //支付失败
                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=0'
                  })
                }
              } else {
                //支付失败
                wx.reLaunch({
                  url: '/pages/paymentResults/paymentResults?codes=0'
                })
              }
            })
          }).catch(err => {
            wx.reLaunch({
              url: '/pages/paymentResults/paymentResults?codes=0'
            })
          })
        })
      } else if (payType == 5008){
        
        http.fetchApi('qqPay/api/zf', { jes: total * 100, lx: 3, uid, order_id: this.data.orderId }).then(payParmms => {

          qq.requestPayment({
            package: payParmms.data.packagess,
            success: () => {
              http.fetchApi('procedure_t', {
                p: 'R2007002|' + uid + '|' + total + '|' + orderId + '|' + payType
              }).then(res => {
               
                if (res.data.code == 1) {
                  if (res.data.results[0].codes == 1) {
                    wx.reLaunch({
                      url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
                    })

                  } else {
                    console.log(codes,'不等于1')
                    //支付失败
                    wx.reLaunch({
                      url: '/pages/paymentResults/paymentResults?codes=0'
                    })
                  }
                } else {
                  //支付失败
                  console.log(code, '不等于1')
                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=0'
                  })
                }
              })
            },
            fail: err => {
              console.log('弹窗没吊起来')
              // 支付失败
              wx.reLaunch({
                url: '/pages/paymentResults/paymentResults?codes=0'
              })
            }
          })
      
        })
      } else {
        http.fetchApi('procedure_t', {
          p: 'R2007002|' + uid + '|' + total + '|' + orderId + '|' + payType
        }).then(res => {
          if (res.data.code == 1) {
            if (res.data.results[0].codes == 1) {
              wx.reLaunch({
                url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
              })

            } else {
              //支付失败
              wx.reLaunch({
                url: '/pages/paymentResults/paymentResults?codes=0'
              })
            }
          } else {
            //支付失败
            wx.reLaunch({
              url: '/pages/paymentResults/paymentResults?codes=0'
            })
          }
        })
      }
     
    } else {
      //正常下单流程

      //微信支付
      if (payType == 5001) {
        if (total==0){
          wx.showToast({
            title: '金额为0元时，无法使用微信支付，请使用其他支付方式',
            icon:'none'
          })
          return
        }
        http.fetchApi('api/zf', { jes: total * 100, lx: 2, uid, order_id: this.data.orderId }).then(payParmms => {
          var t = payParmms.data.timestampss,
            n = payParmms.data.nonceStrss,
            p = payParmms.data.packagess,
            s = payParmms.data.signTypess,
            ps = payParmms.data.paySignss;
          http.payment(t, n, p, s, ps).then(() => {
            http.fetchApi('p_order_fbdd', { p: 'R2002005|' + uid + '|' + orderId + '|' + total + '|' + payType + '|' + redid }).then(res => {
              if (res.data.code == 1) {
                //支付成功
                if (res.data.results[0].codes == 1) {
                  wx.hideLoading();
                  var pages = getCurrentPages();
                  //上一个页面实例对象
                  var prePage = pages[pages.length - 2];
                  console.log(prePage.route)
                  if (prePage.route == 'pages/takeExpress/takeExpress') {

                    //如果是加单订单的话 清除加单购物车和总价 优惠券信息
                    wx.removeStorageSync('shopCar1');
                    wx.removeStorageSync('totalPrice');
                    wx.removeStorageSync('jiadanId');
                    wx.removeStorageSync('hongbao');
                  }

                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
                  })

                } else {
                  //支付失败
                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=0'
                  })
                }
              } else {
                //支付失败
                wx.reLaunch({
                  url: '/pages/paymentResults/paymentResults?codes=0'
                })
              }


            })
          }).catch(err => {
            wx.reLaunch({
              url: '/pages/paymentResults/paymentResults?codes=0'
            })
          })
        })
      } else if (payType == 5008) {
        if (total == 0) {
          wx.showToast({
            title: '金额为0元时，无法使用QQ支付，请使用其他支付方式',
            icon: 'none'
          })
          return
        }
        http.fetchApi('qqPay/api/zf', { jes: total * 100, lx: 3, uid, order_id: this.data.orderId }).then(payParmms => {
          
          qq.requestPayment({
            package: payParmms.data.packagess,
            success:()=>{
              http.fetchApi('p_order_fbdd', { p: 'R2002005|' + uid + '|' + orderId + '|' + total + '|' + payType + '|' + redid }).then(res => {
                //console.log('进入R2002005接口参数是', uid, orderId, total, payType, redid)
                if (res.data.code == 1) {
                 // console.log('进入code==1的逻辑')
                  //支付成功
                  if (res.data.results[0].codes == 1) {
                   // console.log('进入codes==1的逻辑')
                    wx.hideLoading();
                    var pages = getCurrentPages();
                    //上一个页面实例对象
                    var prePage = pages[pages.length - 2];
                    console.log(prePage.route)
                    if (prePage.route == 'pages/takeExpress/takeExpress') {

                      //如果是加单订单的话 清除加单购物车和总价 优惠券信息
                      wx.removeStorageSync('shopCar1');
                      wx.removeStorageSync('totalPrice');
                      wx.removeStorageSync('jiadanId');
                      wx.removeStorageSync('hongbao');
                    }

                    wx.reLaunch({
                      url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
                    })


                  } else {
                   // console.log('进入codes!=1的逻辑')
                    //支付失败
                    wx.reLaunch({
                      url: '/pages/paymentResults/paymentResults?codes=0'
                    })
                  }
                } else {
                  //支付失败
                  //console.log('进入code!=1的逻辑')
                  wx.reLaunch({
                    url: '/pages/paymentResults/paymentResults?codes=0'
                  })
                }


              }).catch(err=>{
                console.log('R2002005接口失败支付失败'+err)
              })
              
            },
            fail:err=> {
              console.log('弹窗没吊起来' + err)
             // 支付失败
              wx.reLaunch({
                url: '/pages/paymentResults/paymentResults?codes=0'
              })
             }
          })

    
        })
      } else {
        //金币 余额支付
        http.fetchApi('p_order_fbdd', {
          p: 'R2002005|' + uid + '|' + orderId + '|' + total + '|' + payType + '|' + redid
        })
          .then(res => {
            if (res.data.code == 1) {
              //支付成功
              if (res.data.results[0].codes == 1) {
                wx.hideLoading();
                var pages = getCurrentPages();
                //上一个页面实例对象
                var prePage = pages[pages.length - 2];
                console.log(prePage.route)
                if (prePage.route == 'pages/takeExpress/takeExpress') {

                  //如果是加单订单的话 清除加单购物车和总价 优惠券信息
                  wx.removeStorageSync('shopCar1');
                  wx.removeStorageSync('totalPrice');
                  wx.removeStorageSync('jiadanId');
                  wx.removeStorageSync('hongbao');
                }

                wx.reLaunch({
                  url: '/pages/paymentResults/paymentResults?codes=1&total=' + total + '&orderId=' + orderId
                })



              } else {
                //支付失败
                //  http.showToast(res.data.results[0].nrs + '', '', 'none')
                wx.reLaunch({
                  url: '/pages/paymentResults/paymentResults?codes=0'
                })
              }
            } else {
              //支付失败

              wx.reLaunch({
                url: '/pages/paymentResults/paymentResults?codes=0'
              })
            }


          })
      }
    }
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      orderId: options.orderId,
      total: options.total,
      redid: options.redid || 0,
      ddId: options.ddId,
      isXiaofei: options.isXiaofei,
      changJ:app.globalData.cj || ''
    })

    this.getExchange();
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  }
})
